package com.ete.service.dialog

import android.view.LayoutInflater
import android.view.View
import com.blankj.utilcode.util.ScreenUtils
import com.ete.service.entry.dialog.CommonDialogEntry
import com.ete.service.service.R
import com.ete.service.service.databinding.DialogCommonTipBinding

/**
 * Author:LiXian
 * <p>
 * Desc:通用对话框
 * Version:1.0
 * date 2023/12/20 14:23
 */
class CommonDialog(var mDialogEntry: CommonDialogEntry)
    : BaseDialog(mDialogEntry.mContext, R.style.XUIDialog_Custom) {

    private val mBinding = DialogCommonTipBinding.inflate(LayoutInflater.from(context))

    fun initDialog() {
        if (!mDialogEntry.isOutTouchCancel) {
            setCanceledOnTouchOutside(false)
            setCancelable(false)
        }
        setContentView(mBinding.root)
        initView(mBinding)
    }

    private fun initView(mBinding: DialogCommonTipBinding) {
        mBinding.mEntry = mDialogEntry
        mBinding.dialogBtnOk.updateThemeColor(mDialogEntry.themeColor)
        mBinding.dialogBtnCancel.setOnClickListener {
            if (mDialogEntry.mCancelCallback == null) {
                dismiss()
            } else {
                val isDismiss: Boolean = mDialogEntry.mCancelCallback!!.invoke()
                if (isDismiss) dismiss()
            }
        }
        mBinding.dialogIvClose.setOnClickListener {
            if (mDialogEntry.mCancelCallback == null) {
                dismiss()
            } else {
                val isDismiss: Boolean = mDialogEntry.mCancelCallback!!.invoke()
                if (isDismiss) dismiss()
            }
        }
        mBinding.dialogBtnOk.setOnClickListener {
            if (mDialogEntry.mSureCallback == null) {
                dismiss()
            } else {
                val isDismiss: Boolean = mDialogEntry.mSureCallback!!.invoke()
                if (isDismiss) dismiss()
            }
        }
    }

    fun updateContent(content: String) {
        mBinding.dialogTvContent.text = content
    }

    fun updateTouchCancel(isTouchCancel: Boolean) {
        setCanceledOnTouchOutside(isTouchCancel)
        setCancelable(isTouchCancel)
    }

    fun updateCancelCallback(mCallback: () -> Boolean) {
        mDialogEntry.mCancelCallback = mCallback
    }

    fun updateTitle(mTitle: String) {
        mBinding.dialogTvTitle.text = mTitle
    }

    fun showLoadingView() {
        mBinding.dialogLvLoading.visibility = View.VISIBLE
    }

    fun isShowCancelBtn(isShow: Boolean) {
        mBinding.dialogBtnCancel.visibility = if (isShow) View.VISIBLE else View.GONE
    }

    override fun getDialogWidthRate(): Float {
        return if (ScreenUtils.isLandscape()) {
            0.45F
        } else {
            0.55F
        }
    }
}